package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.wxm.servlet.BaseServlet;
import com.xm.dao.BannerDaoImpl;
import com.xm.entity.Banners;
import com.xm.uti.Result;

/**
 * Servlet implementation class BannerServletAdmin
 */
@WebServlet("/admin/BannerServletAdmin")
public class BannerServletAdmin extends BaseServlet {
	BannerDaoImpl bannerdao = new BannerDaoImpl();

	/**
	 * 获取轮播图列表（全查）
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、调用dao层方法获取数据，
		ArrayList<Banners> banList = bannerdao.getBanList();
		// 将数据封装为layui需要的格式
		String res = Result.toClient(0, "轮播图列表", banList, banList.size());

		// 2、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	/**
	 * 添加方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insertBanner(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、从页面上拿到数据
		String title = req.getParameter("title");// 表单中标签name的值
		String url = req.getParameter("url");
		String link = req.getParameter("link");
		String weight = req.getParameter("weight");
		

		// 2、调用dao层方法，将数据添加到数据库中
		Banners banners = new Banners(0, title, url, link, Integer.valueOf(weight));
		boolean res = bannerdao.insertBan(banners);

		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();

		if (res) {
			out.write(Result.toClient(0, "添加成功"));
		} else {
			out.write(Result.toClient(1, "添加失败"));
		}
		// 将数据封装为layui需要的格式
		out.flush();
		out.close();
	}

	/**
	 * 修改轮播图信息
	 */
	public void updateBan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.接收客户端传来的参数(字符串类型)
		String title = req.getParameter("title");
		String url = req.getParameter("url");
		String link = req.getParameter("link");
		String weight = req.getParameter("weight");
		String id = req.getParameter("id");

		// 2.调用dao层的方法，修改
		boolean res = bannerdao.updateBan(new Banners(Integer.valueOf(id), title, url, link, Integer.valueOf(weight)));
		System.out.println("要修改的轮播图对象是" + res);
		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();

		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		// 将数据封装为layui需要的格式
		out.flush();
		out.close();
	}

	/**
	 * 删除方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delBanById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取客户端传过来的参数
		String id = req.getParameter("id");
		if (id != null && !id.equals("")) {
			int id2 = Integer.valueOf(id);// 将字符串转换为整数类型
			// 2.调用dao层的方法，删除
			boolean res = bannerdao.delBanner(id2);
			// 3、讲数据返回到页面上
			PrintWriter out = resp.getWriter();

			if (res) {
				out.write(Result.toClient(0, "删除成功"));
			} else {
				out.write(Result.toClient(1, "删除失败"));
			}
			// 将数据封装为layui需要的格式
			out.flush();
			out.close();
		}
	}

}
